package com.htc.adapter;

import java.io.File;
import java.util.ArrayList;

import com.htc.adapter.NewsAdapter.viewHolder;
import com.htc.constants.Constants;
import com.htc.dlc.R;
import com.htc.dlc.SettingsActivity;
import com.htc.model.dto.EventsDTO;
import com.htc.webservice.PicassoUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class EventsModelAdapter extends ArrayAdapter<Object> {
	Context mContext;
	private ArrayList<Object> arrObject;
	
	public EventsModelAdapter(Context context, int resource,
			ArrayList<Object> arrData) {
		super(context, resource, arrData);
		mContext = context;
		setArrObject(arrData);

	}

	class viewHolder {
		ImageView imgEventsModel;
		TextView txtEventsModelTitle;
		TextView txtEventsModelDescription;
	}

	public ArrayList<Object> getArrObject() {
		return arrObject;
	}

	public void setArrObject(ArrayList<Object> arrObject) {
		this.arrObject = arrObject;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return getArrObject().size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return getArrObject().get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.adapter_events, parent, false);

		viewHolder holder = new viewHolder();
		holder.imgEventsModel = (ImageView) rowView
				.findViewById(R.id.imgEventsModel);
		holder.txtEventsModelTitle = (TextView) rowView
				.findViewById(R.id.txtEventModelTitle);
		holder.txtEventsModelDescription = (TextView) rowView

		.findViewById(R.id.txtEventsModelDescription);
		final EventsDTO dto = (EventsDTO) getArrObject().get(position);
		SettingsActivity st = new SettingsActivity();
		if(st.flag == 0){
			holder.txtEventsModelTitle.setText(dto.getEventTitle());
			holder.txtEventsModelDescription.setText(dto.getEventDescription());
		}else if(st.flag == 1){
			holder.txtEventsModelTitle.setText(dto.getEventTitleAr());
			holder.txtEventsModelDescription.setText(dto.getEventDescriptionAr());
		}
		
		String path = dto.getEventImage();
		String currentUrl = Constants.URL_IMAGE_PAHT + path;
		
		if(path != null){
			String[] strs = path.split("/");
			File f = new File(Environment.getExternalStorageDirectory() + "/"
					+ Constants.APP_NAME + "/" + strs[0] + "/" + strs[1]);
			if (f.exists()) {
				if(f.length() == 0){
					f.delete();
					PicassoUtils.getInstance().showImageFromUrl(mContext, currentUrl,
							R.drawable.bgday, holder.imgEventsModel);
					// TODO: create folder?

					PicassoUtils.getInstance().saveImageFromUrl(
							mContext.getApplicationContext(), currentUrl, strs[1],
							strs[0]);
				}else{
					Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
					holder.imgEventsModel.setImageBitmap(bmp);
				}

			} else {
				PicassoUtils.getInstance().showImageFromUrl(mContext, currentUrl,
						R.drawable.bgday, holder.imgEventsModel);
				// TODO: create folder?

				PicassoUtils.getInstance().saveImageFromUrl(
						mContext.getApplicationContext(), currentUrl, strs[1],
						strs[0]);
			}

		}
		
		
		return rowView;
	}
}
